<?php

/*
 * Action - Attack
 * 
 * +$1
 * 
 * When you buy this or play it, each other player reveals the top 2 cards of his deck,
 * trashes a revealed Silver or Gold you choose, and discards the rest.
 * If he didn’t reveal a Treasure, he gains a Copper. You gain the trashed cards.
 */

class NobleBrigand extends Card implements iKingdom, iHinterlands, iAction, iAttack {

    public function __construct() {
        parent::__construct("Noble Brigand", 4);
    }

    public function getDescription() {
        return array(
            "+$1",
            "When you buy this or play it, each other player reveals the top 2 cards of his deck, trashes a revealed Silver or Gold you choose, and discards the rest.",
            "If he didn’t reveal a Treasure, he gains a Copper. You gain the trashed cards.");
    }

    public function play(Game $game, $isRealCard = true) {
        $game->addMoney(1);
        ScreenDisplay::addScreenMessage($game, true, "get +$1", "gets +$1");

        $game->attack($this);
        $this->nobleBrigandAction($game, true);

        $game->cardActionFinished();
        return true;
    }

    public function buy(Game $game) {
        $this->nobleBrigandAction($game, false);
    }

    private function nobleBrigandAction(Game $game, $isAttack) {
        $names = $game->getOtherPlayersNames($isAttack);

        foreach ($names as $name) {
            $this->helper($game, $name);
        }
    }

    private function helper(Game $game, $name) {

        $cards = $game->removeCardsFromDrawDeck(2, $name);

        ScreenDisplay::addScreenMessage($game, true, "reveal " . nicePrint($cards), "reveals " . ScreenDisplay::$LMSG, $name);

        $has_treasure_cards = false;
        $silver_or_gold = array();
        $discard_cards = array();
        foreach ($cards as $card) {
            if ($card instanceof iTreasure) {
                $has_treasure_cards = true;
                if (($card instanceof Gold) || ($card instanceof Silver)) {
                    $silver_or_gold[] = $card;
                } else {
                    $discard_cards[] = $card;
                }
            } else {
                $discard_cards[] = $card;
            }
        }

        if (sizeof($discard_cards) > 0) {
            $game->discardTempDiscardCards($discard_cards, $name);
            ScreenDisplay::addScreenMessage($game, true, "discard " . nicePrint($discard_cards), "discards " . ScreenDisplay::$LMSG, $name);
        }
        
        if (!$has_treasure_cards) {
            $copper = $game->moveCardFromPileToPile("Copper", BoardCards, DiscardPile, $name);
            ScreenDisplay::addScreenMessage($game, true, "gain " . nicePrint($copper), "gains " . ScreenDisplay::$LMSG, $name);
        }

        $trashed_card = null;
        //remember that the other player treasure cards are currently in the 'air' we should decide where to put them
        if (sizeof($silver_or_gold) == 2) {

            $output = MySocket::GetUserReply($game, Input_ChooseOne, $silver_or_gold, 1, 1, null, "Choose a card to trash for " . $name);
            $trashed_card = $output[0];
            $trashed_card = n2c($trashed_card);

            $card1_name = $silver_or_gold[0];
            $card2_name = $silver_or_gold[1];
            $other_card = null;
            if ($card1_name->getKey() == $trashed_card->getKey()) {
                $other_card = $card2_name;
            } else if ($card2_name->getKey() == $trashed_card->getKey()) {
                $other_card = $card1_name;
            }
            //we discard the not chosen card
            $game->putCardInDiscardDeck($other_card, $name);
            ScreenDisplay::addScreenMessage($game, true, "discard " . nicePrint($other_card), "discards " . ScreenDisplay::$LMSG, $name);
        } else if (sizeof($silver_or_gold) == 1) {
            $trashed_card = $silver_or_gold[0];
        }

        if ($trashed_card) {
            //trashing the coosen card
            $game->putCardInTrash($trashed_card);
            ScreenDisplay::addScreenMessage($game, true, "trash " . nicePrint($trashed_card), " trashes " . ScreenDisplay::$LMSG, $name);
            $game->moveCardFromPileToPile($trashed_card, Trash, DiscardPile);
            ScreenDisplay::addScreenMessage($game, true, " gain " . nicePrint($trashed_card) . " from trash", " gains " . ScreenDisplay::$LMSG . " from trash");
        }
    }

}

?>
